#!/usr/bin/env python

import sqlite3
import sys

# Receive the argument from bash
filename = sys.argv[1] + '.sqlite'
 
conn = sqlite3.connect(filename)
 
cursor = conn.cursor()
 
# Create database

# Create tables

cursor.execute("""
CREATE TABLE [index] (
    url CHAR PRIMARY KEY,
    status CHAR,
    timestamp DATETIME DEFAULT ( CURRENT_TIMESTAMP )
    );
               """)

cursor.execute("""
CREATE TABLE post (
    url CHAR PRIMARY KEY,
    title TEXT,
    text TEXT,
    date DATETIME,
    timestamp DATETIME DEFAULT ( CURRENT_TIMESTAMP )
    );
               """)

cursor.execute("""
CREATE TABLE comment (
    commment_id INTEGER PRIMARY KEY,
    post_url CHAR,
    text TEXT ,
    username TEXT,
    created DATETIME,
    replied_to INTEGER,
    timestamp DATETIME DEFAULT ( CURRENT_TIMESTAMP ),
    FOREIGN KEY (post_url) REFERENCES url(post)
    );
               """)

# Close connection
conn.commit()
